//
//  InterfaceController.swift
//  Carnivore WatchKit Extension
//
//  Created by macbookair on 11/21/16.
//  Copyright © 2016 swift. All rights reserved.
//

import WatchKit
import Foundation


class InterfaceController: WKInterfaceController {
    
    
    
    @IBOutlet var timer: WKInterfaceTimer!
    
    @IBOutlet var weightLable: WKInterfaceLabel!
    
    @IBOutlet var cookLable: WKInterfaceLabel!
    
    @IBOutlet var timerButton: WKInterfaceButton!
    
    var ounces = 16
    
    var cookTemp = MeatTemprature.medium
    
    var timerRunning = false
    
    var usingMetric = false
    

    override func awake(withContext context: Any?) {
        
        super.awake(withContext: context)
        
        // Configure interface objects here.
        
        updateConfiguration()
    }
    
    
    
    private func updateConfiguration(){
        
        
        // 1
        
        cookLable.setText(cookTemp.stringValue)
        
        var weight = ounces
        
        var unit = "oz"
        
        if usingMetric {
            
            let grams = Double(ounces) * 28.3495
            
            weight = Int(grams)
            
            unit = "gm"
        }
        
        
        weightLable.setText("Weight:\(weight)\(unit)")
    
    }
    
    
    @IBAction func OnTimerButton() {
        
        
        if timerRunning {
            
            timer.stop()
            
            timerButton.setTitle("Start Timer")
            
        } else {
            
            let time = cookTemp.cookTimeForOunces(ounces)
            
            timer.setDate(Date(timeIntervalSinceNow: time))
            
            timer.start()
            
            timerButton.setTitle("Stop Timer")
            
        }
        
        
        timerRunning = !timerRunning
        
    }
    

    @IBAction func OnPlusButton() {
        
        ounces += 1
        
        updateConfiguration()
        
        
    }
    
    
    @IBAction func OnMinusButton() {
        
        ounces -= 1
        
        if ounces <= 0 {
            
            ounces = 0
        }
        
        updateConfiguration()
        
    }
    
    
    @IBAction func OnTempChange(_ value: Float) {
        
        if let temp = MeatTemprature(rawValue:Int(value)) {
        
            cookTemp = temp;

            updateConfiguration()
        }
        
        
    }
    
    
    @IBAction func OnMetricSwitchChange(_ value: Bool) {
        
        usingMetric = value
        
        updateConfiguration()
    }
    
    
    override func willActivate() {
        
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }
    
    override func didDeactivate() {
        
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }
    
    

}
